package com.crm.service.after_sale.impl;

import java.util.List;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.dao.after_sale.ClientCareDao;
import com.crm.dao.client.ClientInfoDao;
import com.crm.dao.client.ContactDao;
import com.crm.pojo.ClientCare;
import com.crm.pojo.ClientInfo;
import com.crm.pojo.Contact;
import com.crm.service.after_sale.ClientCareService;
import com.crm.vo.ClientCareAndContact;
import com.crm.vo.Result;

@Service
@Transactional(rollbackFor = Exception.class)
public class ClientCareServiceImpl implements ClientCareService {

	@Autowired
	private ClientCareDao clientCareDao;
	@Autowired
	private ContactDao contactDao;
	
	@Override
	public ClientCare findOneIsNotDel(Long careid) {
		return clientCareDao.findOneIsNotDel(careid);
	}

	@Override
	public List<ClientCareAndContact> findAllNotDelByPage(int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return clientCareDao.findAllNotDelByPage(page);
	}

	@Override
	public Result addClientCare(ClientCare clientCare) {
		try {
			Contact contact = contactDao.findOne(clientCare.getClientId().longValue());
			clientCare.setOwnId(contact.getClientInfo().getUserId());
			clientCare.setAcceptUserId(contact.getClientInfo().getUserId());
			clientCare.setClientCareIsDel("否");
			clientCareDao.save(clientCare);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，新增客户关怀失败！");
		}
	}

	@Override
	public Result editClientCare(ClientCare clientCare) {
		try {
			Contact contact = contactDao.findOne(clientCare.getClientId().longValue());
			clientCare.setOwnId(contact.getClientInfo().getUserId());
			clientCare.setAcceptUserId(contact.getClientInfo().getUserId());
			clientCare.setClientCareIsDel("否");
			clientCareDao.save(clientCare);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，编辑客户关怀失败！");
		}
	}

	@Override
	public Result delClientCare(Long careid) {
		try {
			ClientCare clientCare = clientCareDao.findOne(careid);
			clientCare.setClientCareIsDel("是");
			clientCareDao.save(clientCare);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，删除客户关怀失败！");
		}
	}

	@Override
	public Long countAllNotDelByPage() {
		return clientCareDao.countAllNotDelByPage();
	}

	@Override
	public List<ClientCareAndContact> findCareByCareid(Long careid, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return clientCareDao.findCareByCareid(careid, page);
	}

	@Override
	public Long countCareByCareid(Long careid) {
		return clientCareDao.countCareByCareid(careid);
	}

	@Override
	public List<ClientCareAndContact> findCareByCareTheme(String careTheme, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return clientCareDao.findCareByCareTheme("%"+careTheme+"%", page);
	}

	@Override
	public Long countCareByCareTheme(String careTheme) {
		return clientCareDao.countCareByCareTheme("%"+careTheme+"%");
	}

}
